package service

import (
	"time"
	"yak.admin/src/entity"
	"yak.admin/src/util"
)

type BehaviorService struct {

}

func (b *BehaviorService) Drug(province string, city string, county string) map[string]interface{} {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	mm := map[string]interface{}{"report": 0, "watch": 0, "unwatch": 0,}
	var reportBehavior []*entity.ReportBehavior
	if db.Where("area = ? and sign in (?)", area, []string{entity.YaoPingReportSign, entity.YaoPingReportSignWatch, entity.YaoPingReportSignUnwatch}).
		Find(&reportBehavior).RowsAffected > 0 {
			for _, val := range reportBehavior {
				if val.Sign == entity.YaoPingReportSign {
					mm["report"] = val.ValueBig
				}
				if val.Sign == entity.YaoPingReportSignWatch {
					mm["watch"] = val.ValueBig
				}
				if val.Sign == entity.YaoPingReportSignUnwatch {
					mm["unwatch"] = val.ValueBig
				}
			}
	}
	return mm
}

func (b *BehaviorService) DrugDaily(province string, city string, county string) map[string]interface{} {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	mm := map[string]interface{}{"report": 0, "watch": 0, "unwatch": 0,}
	var reportBehavior []*entity.ReportBehaviorDaily
	if db.Where("area = ? and sign in (?) and daily = ?", area, []string{entity.YaoPingReportSignDaily, entity.YaoPingReportSignWatchDaily, entity.YaoPingReportSignUnwatchDaily}, time.Now().Format("2006-01-02")).
		Find(&reportBehavior).RowsAffected > 0 {
		for _, val := range reportBehavior {
			if val.Sign == entity.YaoPingReportSignDaily {
				mm["report"] = val.ValueInt
			}
			if val.Sign == entity.YaoPingReportSignWatchDaily {
				mm["watch"] = val.ValueInt
			}
			if val.Sign == entity.YaoPingReportSignUnwatchDaily {
				mm["unwatch"] = val.ValueInt
			}
		}
	}
	return mm
}

func (b *BehaviorService) Device(province string, city string, county string) map[string]interface{} {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	mm := map[string]interface{}{"report": 0, "watch": 0, "unwatch": 0,}
	var reportBehavior []*entity.ReportBehavior
	if db.Where("area = ? and sign in (?)", area, []string{entity.QiXieReportSign, entity.QiXieReportSignWatch, entity.QiXieReportSignUnwatch}).
		Find(&reportBehavior).RowsAffected > 0 {
		for _, val := range reportBehavior {
			if val.Sign == entity.QiXieReportSign {
				mm["report"] = val.ValueBig
			}
			if val.Sign == entity.QiXieReportSignWatch {
				mm["watch"] = val.ValueBig
			}
			if val.Sign == entity.QiXieReportSignUnwatch {
				mm["unwatch"] = val.ValueBig
			}
		}
	}
	return mm
}

func (b *BehaviorService) DeviceDaily(province string, city string, county string) map[string]interface{} {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	mm := map[string]interface{}{"report": 0, "watch": 0, "unwatch": 0,}
	var reportBehavior []*entity.ReportBehaviorDaily
	if db.Where("area = ? and sign in (?) and daily = ?", area, []string{entity.QiXieReportSignDaily, entity.QiXieReportSignWatchDaily, entity.QiXieReportSignUnwatchDaily, time.Now().Format("2006-01-02")}).
		Find(&reportBehavior).RowsAffected > 0 {
		for _, val := range reportBehavior {
			if val.Sign == entity.QiXieReportSignDaily {
				mm["report"] = val.ValueInt
			}
			if val.Sign == entity.QiXieReportSignWatchDaily {
				mm["watch"] = val.ValueInt
			}
			if val.Sign == entity.QiXieReportSignUnwatchDaily {
				mm["unwatch"] = val.ValueInt
			}
		}
	}
	return mm
}


func (b *BehaviorService) WriteMessageTimes(userId uint) {
	db := util.GetMysqlPool().Handler()
	reportBehavior := entity.MessageBehavior{}
	db.Where(entity.MessageBehavior{UserId: userId, Sign: entity.UserMessageSign}).
		Assign(entity.MessageBehavior{ValueInt: 1}).
		FirstOrCreate(&reportBehavior)
}

func (b *BehaviorService) FlushMessageTimes(userId uint) {
	db := util.GetMysqlPool().Handler()
	db.Delete(entity.MessageBehavior{}, "user_id = ? and sign = ?", userId, entity.UserMessageSign)
}

func (b *BehaviorService) ReadMessageTimes(userId uint) int {
	db := util.GetMysqlPool().Handler()
	message := entity.MessageBehavior{}
	db.Where("user_id = ? and sign = ?", userId, entity.UserMessageSign).
		First(&message)
	return message.ValueInt
}

func (b *BehaviorService) GetArea(province string, city string, county string) string {
	return province + "-" + city + "-" + county
}

// 添加管理员通知
func (b *BehaviorService) AddNoticeTimes(province string, city string, county string, t int) {
	area := b.GetArea(province, city, county)
	db := util.GetMysqlPool().Handler()
	reportBehavior := entity.ReportNoticeBehavior{}
	db.Where(entity.ReportNoticeBehavior{Area: area, Type: t}).
		Assign(entity.ReportNoticeBehavior{Times: 1, Status: 0}).
		FirstOrCreate(&reportBehavior)
}

// 药品提交
func (b *BehaviorService) SubmitDrug(province string, city string, county string)  {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	daily := time.Now().Format("2006-01-02")
	// 区域历史药品风险上报总数 + 1
	reportBehavior := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: area, Sign: entity.YaoPingReportSign}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehavior)
	// 区域今日药品风险上报 + 1
	reportBehaviorDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: area, Sign: entity.YaoPingReportSignDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorDaily)
	// 总历史药品风险上报总数 + 1
	reportBehaviorTotal := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: "--", Sign: entity.YaoPingReportSign}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotal)
	// 总今日药品风险上报 + 1
	reportBehaviorTotalDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: "--", Sign: entity.YaoPingReportSignDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotalDaily)
}

func (b *BehaviorService) WatchDrug(province string, city string, county string)  {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	daily := time.Now().Format("2006-01-02")
	// 区域关注药品风险总数 + 1
	reportBehavior := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: area, Sign: entity.YaoPingReportSignWatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehavior)
	// 区域今日关注药品风险总数 + 1
	reportBehaviorDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: area, Sign: entity.YaoPingReportSignWatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorDaily)
	// 总关注药品风险总数 + 1
	reportBehaviorTotal := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: "--", Sign: entity.YaoPingReportSignWatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotal)
	// 总今日关注药品风险总数 + 1
	reportBehaviorTotalDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: "--", Sign: entity.YaoPingReportSignWatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotalDaily)
}

func (b *BehaviorService) UnwatchDrug(province string, city string, county string)  {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	daily := time.Now().Format("2006-01-02")
	// 区域取消关注药品风险总数 + 1
	reportBehavior := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: area, Sign: entity.YaoPingReportSignUnwatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehavior)
	// 区域今日取消关注药品风险总数 + 1
	reportBehaviorDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: area, Sign: entity.YaoPingReportSignUnwatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorDaily)
	// 总取消关注药品风险总数 + 1
	reportBehaviorTotal := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: "--", Sign: entity.YaoPingReportSignUnwatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotal)
	// 总取消今日关注药品风险总数 + 1
	reportBehaviorTotalDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: "--", Sign: entity.YaoPingReportSignUnwatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotalDaily)
}

// 设备提交
func (b *BehaviorService) SubmitDevice(province string, city string, county string)  {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	daily := time.Now().Format("2006-01-02")
	// 区域历史设备风险上报总数 + 1
	reportBehavior := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: area, Sign: entity.QiXieReportSign}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehavior)
	// 区域今日设备风险上报 + 1
	reportBehaviorDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: area, Sign: entity.QiXieReportSignDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorDaily)
	// 总历史设备风险上报总数 + 1
	reportBehaviorTotal := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: "--", Sign: entity.QiXieReportSign}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotal)
	// 总今日设备风险上报 + 1
	reportBehaviorTotalDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: "--", Sign: entity.QiXieReportSignDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotalDaily)
}

func (b *BehaviorService) WatchDevice(province string, city string, county string)  {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	daily := time.Now().Format("2006-01-02")
	// 区域关注药品风险总数 + 1
	reportBehavior := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: area, Sign: entity.QiXieReportSignWatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehavior)
	// 区域今日关注药品风险总数 + 1
	reportBehaviorDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: area, Sign: entity.QiXieReportSignWatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorDaily)
	// 总关注药品风险总数 + 1
	reportBehaviorTotal := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: "--", Sign: entity.QiXieReportSignWatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotal)
	// 总今日关注药品风险总数 + 1
	reportBehaviorTotalDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: "--", Sign: entity.QiXieReportSignWatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotalDaily)
}

func (b *BehaviorService) UnwatchDevice(province string, city string, county string)  {
	db := util.GetMysqlPool().Handler()
	area := b.GetArea(province, city, county)
	daily := time.Now().Format("2006-01-02")
	// 区域取消关注药品风险总数 + 1
	reportBehavior := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: area, Sign: entity.QiXieReportSignUnwatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehavior)
	// 区域今日取消关注药品风险总数 + 1
	reportBehaviorDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: area, Sign: entity.QiXieReportSignUnwatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorDaily)
	// 总取消关注药品风险总数 + 1
	reportBehaviorTotal := entity.ReportBehavior{}
	db.Where(entity.ReportBehavior{Area: "--", Sign: entity.QiXieReportSignUnwatch}).
		Assign(entity.ReportBehavior{ValueBig: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotal)
	// 总取消今日关注药品风险总数 + 1
	reportBehaviorTotalDaily := entity.ReportBehaviorDaily{}
	db.Where(entity.ReportBehaviorDaily{Area: "--", Sign: entity.QiXieReportSignUnwatchDaily, Daily: daily}).
		Assign(entity.ReportBehaviorDaily{ValueInt: 1, Times: 1}).
		FirstOrCreate(&reportBehaviorTotalDaily)
}

